package autoscale;

import java.util.List;
import java.util.ArrayList;
import java.util.Collection;

import Common.TraceWriter;
import cloudjunit.Config;

import com.amazonaws.services.autoscaling.AmazonAutoScaling;
import com.amazonaws.services.autoscaling.model.*;
import com.amazonaws.services.cloudwatch.AmazonCloudWatch;
import com.amazonaws.services.cloudwatch.model.DeleteAlarmsRequest;


public class CloudJUnitAutoScaleDelete
{
	static final String AUTOSCALE_CONF = "cloudjunitConf4";
	static final String AUTOSCALE_GROUP = "cloudjunitScaleGroup4";


	public static void main(String[] args)
	{
		TraceWriter.trace("STARTED.");
		
		// Init stuff
		Config.initConfigWithConfigFile();
		AmazonAutoScaling aasc = Config.createAutoScaleClient();
		AmazonCloudWatch acwc = Config.createCloudWatchClient();
		
		TraceWriter.trace("DeleteAlarmsRequest");
		DeleteAlarmsRequest deleteAlarmsRequest = new DeleteAlarmsRequest();
		Collection<String> alarmNames = new ArrayList<String>();
		alarmNames.add(CloudJUnitAutoScale.HIGH_CPU_ALARM);
		alarmNames.add(CloudJUnitAutoScale.LOW_CPU_ALARM);
		deleteAlarmsRequest.setAlarmNames(alarmNames);
		acwc.deleteAlarms(deleteAlarmsRequest);
		
		//////////////////////////////
		
		TraceWriter.trace("UpdateAutoScalingGroupRequest");
		UpdateAutoScalingGroupRequest updateAutoScalingGroupRequest = new UpdateAutoScalingGroupRequest();
		updateAutoScalingGroupRequest.setAutoScalingGroupName(CloudJUnitAutoScale.AUTOSCALE_GROUP);
		updateAutoScalingGroupRequest.setMinSize(0);
		aasc.updateAutoScalingGroup(updateAutoScalingGroupRequest);
		
		//////////////////////////////
		
		TraceWriter.trace("DescribeAutoScalingInstancesRequest");
		DescribeAutoScalingInstancesRequest describeAutoScalingInstancesRequest = new DescribeAutoScalingInstancesRequest();
		DescribeAutoScalingInstancesResult describeAutoScalingInstancesResult = aasc.describeAutoScalingInstances(describeAutoScalingInstancesRequest);
		List<AutoScalingInstanceDetails> instanceList = describeAutoScalingInstancesResult.getAutoScalingInstances();
		for (AutoScalingInstanceDetails instaceDetail : instanceList)
		{
			TraceWriter.trace("Terminating: " + instaceDetail.getInstanceId());
			TerminateInstanceInAutoScalingGroupRequest terminateInstanceInAutoScalingGroupRequest = new TerminateInstanceInAutoScalingGroupRequest();
			terminateInstanceInAutoScalingGroupRequest.setInstanceId(instaceDetail.getInstanceId());
			terminateInstanceInAutoScalingGroupRequest.setShouldDecrementDesiredCapacity(true);
			TerminateInstanceInAutoScalingGroupResult result = aasc.terminateInstanceInAutoScalingGroup(terminateInstanceInAutoScalingGroupRequest);
			TraceWriter.trace("Termination result: " + result.toString());
		}
		
		//////////////////////////////
		
		TraceWriter.trace("DeleteAutoScalingGroupRequest");
		DeleteAutoScalingGroupRequest deleteAutoScalingGroupRequest = new DeleteAutoScalingGroupRequest();
		deleteAutoScalingGroupRequest.setAutoScalingGroupName(CloudJUnitAutoScale.AUTOSCALE_GROUP);
		aasc.deleteAutoScalingGroup(deleteAutoScalingGroupRequest);
		
		TraceWriter.trace("DeleteLaunchConfigurationRequest");
		DeleteLaunchConfigurationRequest deleteLaunchConfigurationRequest = new DeleteLaunchConfigurationRequest();
		deleteLaunchConfigurationRequest.setLaunchConfigurationName(CloudJUnitAutoScale.AUTOSCALE_CONF);
		aasc.deleteLaunchConfiguration(deleteLaunchConfigurationRequest);		
		
		////////////////////////////////
		
		TraceWriter.trace("FINISHED.");
		
	}

}
